<?php
class ProveedoresController extends AppController {

	var $name = 'Proveedores';
	var $components = array('RequestHandler');
	
	function index(){// Configure::write('debug',1);
		$this->loadModel('Proveedor');
		$this->layout="ajax";
					
		if($this->JQGridPaginate->isGrid){
			$fields = array('Proveedor.id','Proveedor.codigo','Proveedor.razonsocial','Proveedor.nombrecomercial',
							'Proveedor.direccion','Proveedor.ruc','Proveedor.dni','Proveedor.fechainicio',
							'Proveedor.telefono','Proveedor.fax','Proveedor.email','Proveedor.localidad',
							'Proveedor.appaterno','Proveedor.apmaterno','Proveedor.nombres','Proveedor.paginaweb',
							'Proveedor.rubro',
							'"Proveedor"."created"::date AS "Proveedor__created"',
							'"Estado"."descripcion" AS "Proveedor__estado"');
			debug($this->Proveedor->find('first', array('fields'=>$fields)));
			$this->JQGridPaginate->recursive = 0;
			$this->JQGridPaginate->cakefields = $fields;
			
			$this->autoRender = false;
			
			debug($this->JQGridPaginate->paginate("Proveedor"));
			echo $this->JQGridPaginate->paginate("Proveedor");
			$this->autoRender = false;
			//die;
		}elseif ($this->RequestHandler->isPost()){			
			$this->autoRender = false;
			$oper = $this->params['form']['oper'];
			if($oper == "add"){
				$this->data['Proveedor']['organizacion_id'] = 1;
				unset($this->data['Proveedor']['fechainicio']);
				$this->Proveedor->create();				 
			}elseif ($oper == "edit"){
				unset($this->data['Proveedor']['fechainicio']);
				$this->data['Proveedor']['id'] = $this->params['form']['id'];
			}elseif ($oper == "del"){
				$this->data['Proveedor']['id'] = $this->params['form']['id'];
				$this->data['Proveedor']['estado_id'] = 2;				
			}
			
			@$this->Proveedor->save($this->data);
			echo json_encode($this->Proveedor->visualizarError($this->Proveedor->validationErrors));
			$this->autoRender = false;	
		}		
	}
	
	function listaproveedores(){
		$this->loadModel('Proveedor');
		$this->layout="ajax";
		$listaProveedores = $this->Proveedor->find('list',array('fields'=>array('Proveedor.id','Proveedor.razonsocial'),
															'order'=>'Proveedor.razonsocial'));
		echo json_encode($listaProveedores);
		$this->autoRender = false;
	}	
	function listabancos(){
		$this->loadModel('Proveedor');
		$this->layout="ajax";
		$listaProveedores = $this->Proveedor->find('list',
		array('fields'=>array('Proveedor.id','Proveedor.razonsocial'),
		'conditions'=>array('Proveedor.rubro'=>'Bancos'),
		'order'=>'Proveedor.razonsocial'));
		echo json_encode($listaProveedores);
		$this->autoRender = false;
	}	
}